package com.example.coreshopapp.http;


import android.util.Log;

import com.example.coreshopapp.app.Constants;
import com.example.coreshopapp.net.BaseApplication;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class HttpUtils {
    private static ApiService apiService = BaseApplication.getInstance().getRetrofit().create(ApiService.class);
    private static Gson gson = new Gson();

    public static <T> void getRequest(String url, final ApiCallback<T> callback) {
        Call<BaseResponse<Object>> call = apiService.get(url);
        call.enqueue(new Callback<BaseResponse<Object>>() {
            @Override
            public void onResponse(Call<BaseResponse<Object>> call, Response<BaseResponse<Object>> response) {
                if (response.isSuccessful()) {
                    BaseResponse<Object> baseResponse = response.body();
                    if (baseResponse != null && baseResponse.getCode() == 200) {
                        //callback.onSuccess(baseResponse.getData());
                    } else {
                        callback.onFailure("Request failed with code: " + (baseResponse != null ? baseResponse.getCode() : "unknown"));
                    }
                } else {
                    callback.onFailure("Request failed with HTTP code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<Object>> call, Throwable t) {
                callback.onFailure("Network error: " + t.getMessage());
            }
        });
    }

    public static <T> void postRequest(String url, String json, final Type clazz, final ApiCallback<T> callback) {
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, json); // datainfo json传递请求参数
            String token = Constants.AppConstVars.token;
            Call<BaseResponse<Object>> call = apiService.post(url, body,"Bearer "+token);
            call.enqueue(new Callback<BaseResponse<Object>>() {
                @Override
                public void onResponse(Call<BaseResponse<Object>> call, Response<BaseResponse<Object>> response) {
                    if (response.isSuccessful()) {
                        BaseResponse<Object> baseResponse = response.body();
                        if (baseResponse != null && baseResponse.getCode() == 0) {
                            Object data = baseResponse.getData();
                            if (data != null) {
                                String dataStr = data.toString();
                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                gson = new GsonBuilder().setDateFormat(dateFormat.toPattern()).create();
                                T result = gson.fromJson(dataStr, clazz);
                                callback.onSuccess(result);
                            }

                        } else {
                            callback.onFailure("Request failed with code: " + (baseResponse != null ? baseResponse.getCode() : "unknown"));
                        }
                    } else {
                        callback.onFailure("Request failed with HTTP code: " + response.code());
                    }
                }
                @Override
                public void onFailure(Call<BaseResponse<Object>> call, Throwable t) {
                    callback.onFailure("Network error: " + t.getMessage());
                }
            });
        } catch (Exception e) {

            Log.d("ssss",e.getMessage());


        }
    }


}
